package com.hf.wujian.utils;

import java.io.File;
import java.util.List;

public class FileUtils {

    private static ClassLoader classLoader = ClassLoader.getSystemClassLoader();

    //遍历目录下所有class文件
    public static void scanFileToClassList(List<Class> classes,File file,String packageName){
        if(file.exists()){
            //如果是文件夹，继续递归
            if (file.isDirectory()) {
                for (File file1 : file.listFiles()) {
                    String thisPackgeName= packageName+"."+file1.getName();
                    scanFileToClassList(classes,file1,thisPackgeName);
                }
            }else{//如果是文件，将后缀为.class的文件反射后加入list
                if(packageName.endsWith(".class")){
                    String className = packageName.split(".class")[0];
                    try {
                        Class mclass = Class.forName(className);
                        classes.add(mclass);
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
